package com.cyandpos.pad.sale.presenter;

import com.cyandpos.pad.posdata.DjdData;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.Member;
import com.cyandpos.pad.posdata.Person;
import com.cyandpos.pad.posdata.PromationoodInfo;
import com.cyandpos.pad.posdata.QueryGoodParam;
import com.cyandpos.pad.posdata.QueryMemberReq;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.SubTicketItem;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.sale.model.ISaleModel;
import com.cyandpos.pad.sale.model.SaleModelImpl;
import com.cyandpos.pad.sale.view.ISaleView;

import java.util.List;

public class SalePresenterImpl{
    public static final int CREATEORDERNO = 1;
    public static  final int QUERYPERSON = 2;
    public static  final int QUERYGOODS = 3;
    public static  final int FRONTDISCOUNT = 4;
    public static  final int QUERYMEMBER = 5;
    public static  final int CANLEORDER = 6;
    public static  final int PREPAREPAY = 7;
    public static  final int QUERYCODE = 8;

    private ISaleView saleView;
    private ISaleModel saleModel;

    public SalePresenterImpl(ISaleView saleView) {
        this.saleView = saleView;
        this.saleModel = new SaleModelImpl();
    }

    /**
     * 创建订单
     * @param orderNo 1 为负数退货创建订单
     */
    public void createSaleId(String orderNo) {
        saleView.showProgress("创建订单号...");
        saleModel.createOrderNo(orderNo, new ISaleModel.onCreateOrderNoListener() {
            @Override
            public void onCreateOrderNoSuccess(String orderNo) {
                saleView.dismissProgress();
                saleView.createOrderNO(orderNo);
            }

            @Override
            public void onCreateOrderNoFailure(String errorMsg) {
                saleView.dismissProgress();
                saleView.loadFail(errorMsg, CREATEORDERNO);
            }
        });
    }

    /**
     * 查询营业员
     * @param personCode
     */
    public void queryPerson(String personCode,String orderNo) {
        saleView.showProgress("查询营业员...");
        saleModel.queryPerson(personCode,orderNo, new ISaleModel.OnQueryPersonListener() {
            @Override
            public void onQueryPersonSuccess(Person person) {
                saleView.dismissProgress();
                saleView.addPerson(person);
            }

            @Override
            public void onQueryPersonFailure(String errorMsg) {
                saleView.dismissProgress();
                saleView.loadFail(errorMsg, QUERYPERSON);
            }
        });
    }

    /**
     * 商品信息
     * @param goodsCode
     * @param ticketInfo
     * @param subInx 子小票下标
     */
    public void queryGoods(QueryGoodParam goodsCode, TicketInfo ticketInfo, int subInx) {
        saleView.showProgress("查询商品...");
        saleModel.queryGoods(goodsCode, ticketInfo, subInx, new ISaleModel.OnQueryGoodsInfoListener() {
            @Override
            public void onQueryGoodsInfoSuccess(Goods goods) {
                saleView.dismissProgress();
                saleView.handleGoods(goods);
            }

            @Override
            public void onQueryGoodsInfoFailure(String errorMsg) {
                saleView.dismissProgress();
                saleView.loadFail(errorMsg, QUERYGOODS);
            }
        });
    }

    /**
     * 前台折扣
     * @param disType
     * @param money
     * @param goods
     * @param orderNo
     * @param isSingle
     */
    public void frontDiscount(String disType, String money, Goods goods, String orderNo, boolean isSingle) {
        saleView.showProgress("查询折扣...");
        saleModel.frontDiscount(disType, money, goods, orderNo, isSingle, new ISaleModel.OnFrontDiscountListener() {
            @Override
            public void onFrontDiscountSuccess(List<Goods> goods, boolean isSingle) {
                saleView.dismissProgress();
                saleView.frontDiscount(goods,isSingle);
            }

            @Override
            public void onFrontDiscountFailure(String errorMsg) {
                saleView.dismissProgress();
                saleView.loadFail(errorMsg, FRONTDISCOUNT);
            }
        });
    }

    /***
     * 查询会员
     * @param requestParam
     */
    public void queryMember(QueryMemberReq requestParam) {
        saleView.showProgress("查询会员...");
        saleModel.queryMember(requestParam, new ISaleModel.OnQueryMemberListener() {
            @Override
            public void OnQueryMemberSuccess(Member member) {
                saleView.dismissProgress();
                saleView.addMember(member);
            }

            @Override
            public void OnQueryMemberFailure(String errorMsg) {
                saleView.dismissProgress();
                saleView.loadFail(errorMsg, QUERYMEMBER);
            }

            @Override
            public void OnRegisterMemberFailure(QueryMemberReq memberReq,String err) {
                saleView.dismissProgress();
                saleView.onRegisterMemberFailure(memberReq,err);
            }
        });
    }

    /**
     * 取消订单
     * @param orderNo
     */
    public void cancelOrder(String orderNo) {
        saleView.showProgress("取消订单...");
        saleModel.cancelOrder(orderNo, new ISaleModel.onCancelOrderListener() {
            @Override
            public void onCancelOrderSuccess(String orderNo) {
                saleView.dismissProgress();
                saleView.cancelOrder(orderNo);
            }

            @Override
            public void onCancelOrderFailure(String errorMsg) {
                saleView.dismissProgress();
                saleView.loadFail(errorMsg, CANLEORDER);
            }
        });
    }

    /**
     * 查询开单商品
     * @param queryOrderReq
     */
    public void queryOrder(QueryOrderReq queryOrderReq) {
        saleView.showProgress("查询开单商品...");
        saleModel.queryOrder(queryOrderReq, new ISaleModel.onQueryOrderlListener() {
            @Override
            public void onQueryOrderSuccess(List<SubTicketItem> subTicketsBeans) {
                saleView.dismissProgress();
//                saleView.queryOrder(subTicketsBeans);
            }

            @Override
            public void onQueryOrderFailure(String errorMsg) {
                saleView.dismissProgress();
                saleView.loadFail(errorMsg, QUERYCODE);
            }
        });

    }


    /**
     * 准备付款
     * @param ticketInfo
     */
    public void preparePay(TicketInfo ticketInfo, boolean isPromotion, List<PromationoodInfo> exchangeGoodBillInfoList, List<PromationoodInfo> giftGoodsBillList, int promotionGoodsSelectSign) {
        saleView.showProgress("准备付款...");
        saleModel.onPreparePay(ticketInfo, isPromotion,exchangeGoodBillInfoList,giftGoodsBillList,promotionGoodsSelectSign,new ISaleModel.onPreparePayListener() {
            @Override
            public void onPreparePaySuccess(TicketInfo ticketInfo,boolean isPromotion, List<PromationoodInfo> exchangeGoodBillInfoList, List<PromationoodInfo> giftGoodsBillList) {
                saleView.dismissProgress();
                saleView.preparePay(ticketInfo,isPromotion,exchangeGoodBillInfoList,giftGoodsBillList);
            }

            @Override
            public void onPreparePayFailure(String errorMsg) {
                saleView.dismissProgress();
                saleView.loadFail(errorMsg, PREPAREPAY);
            }
        });
    }
}
